package org.jeecg.modules.simulator.model.line.station.strategy;

import org.jeecg.modules.simulator.model.line.station.equipment.StationEntryExitGate;
import org.jeecg.modules.simulator.model.person.Person;

import java.time.LocalTime;
import java.util.*;

/**
 * @program: gp2
 * @description:
 * @author: renzhg
 * @create: 2024-09-11 10:56
 **/
public class GateLimitSpeedStage implements ILimitSpeedStage {
    @Override
    public List<Person> computedLimitPerson(Map<String, StationEntryExitGate> entryExitGateMap, LocalTime currentTime, LocalTime now) {
        List<Person> personList = new ArrayList<>();

        for (Map.Entry<String,StationEntryExitGate> entryExitGate:entryExitGateMap.entrySet()){
            StationEntryExitGate gate = entryExitGate.getValue();
            int limitSpeed = gate.getLimitSpeed();
            int willInputSize=gate.getWillEntryPersonMap().size();

            long secondsDifference =1l;
            long limitNums = limitSpeed * secondsDifference;

            if(limitNums>willInputSize){
                List<Person> person1List = new ArrayList<>(gate.getWillEntryPersonMap().values());
                personList.addAll(person1List);
                person1List.forEach(person -> {
                    gate.getWillEntryPersonMap().remove(person.getId());
                });
            }else{
                List<Person> person1List = new ArrayList<>(gate.getWillEntryPersonMap().values());
                personList.addAll(person1List.subList(0, (int) limitNums));
                person1List.subList(0, (int) limitNums).forEach(person -> {
                    gate.getWillEntryPersonMap().remove(person.getId());
                });
            }
        }
        return personList;
    }
}
